package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.kgc.novel.common.Res;
import com.kgc.novel.domain.entity.BookDiscount;
import com.kgc.novel.domain.entity.BookOrder;
import com.kgc.novel.domain.entity.BookUser;
import com.kgc.novel.domain.param.BookOrderParam;
import com.kgc.novel.domain.param.BookUserParam;
import com.kgc.novel.domain.vo.BookOrderVO;
import com.kgc.novel.feign.OrderUserFeign;
import com.kgc.novel.mapper.BookOrderMapper;
import com.kgc.novel.service.order.BookOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author jcq
 * @since 2023-05-17
 */
@Service("bookOrderService")
public class BookOrderServiceImpl extends MPJBaseServiceImpl<BookOrderMapper, BookOrder> implements BookOrderService {

    @Resource
    private BookOrderMapper bookOrderMapper;

    @Resource
    private OrderUserFeign orderUserFeign;

    /**
     * 查询订单列表
     *
     * @param bookOrderParam 查询参数
     * @return 结果
     */
    @Override
    public Res list(BookOrderParam bookOrderParam) {
        String nickName = bookOrderParam.getNickName();
        List<String> userIds = null;
        if (StringUtils.isNotBlank(nickName)) {
            BookUserParam bookUserParam = new BookUserParam();
            bookUserParam.setNickName(nickName);
            List<BookUser> userList = orderUserFeign.listUser(bookUserParam).getData();
            if (userList.size() > 0) {
                userIds = userList.stream().map(BookUser::getId).toList();
            }
        }
        MPJLambdaWrapper<BookOrder> wrapper = JoinWrappers.lambda(BookOrder.class).selectAll(BookOrder.class)
                .select(BookUser::getNickName)
                .select(BookDiscount::getTitle)
                .leftJoin(BookUser.class, BookUser::getId, BookOrder::getUserId)
                .leftJoin(BookDiscount.class, BookDiscount::getId, BookOrder::getDiscountId)
                // 会员折扣
                .eq(Objects.nonNull(bookOrderParam.getDiscountId()), BookOrderParam::getDiscountId, bookOrderParam.getDiscountId())
                // 用户id
                .in(Objects.nonNull(userIds), BookOrder::getUserId, userIds)
                // 订单状态
                .eq(Objects.nonNull(bookOrderParam.getOrderStatus()), BookOrder::getOrderStatus, bookOrderParam.getOrderStatus())
                // 支付方式
                .eq(Objects.nonNull(bookOrderParam.getOrderWay()), BookOrder::getOrderWay, bookOrderParam.getOrderWay());

        if (Objects.nonNull(bookOrderParam.getPageNo()) && Objects.nonNull(bookOrderParam.getPageSize())) {
            Page<BookOrderVO> listPage = bookOrderMapper.selectJoinPage(new Page<>(2, 10), BookOrderVO.class, wrapper);
            return Res.returnData(listPage.getRecords(), listPage.getTotal());
        } else {
            List<BookOrderVO> bookOrderVOS = bookOrderMapper.selectJoinList(BookOrderVO.class, wrapper);
            return Res.returnData(bookOrderVOS);
        }
    }
}
